package com.zjeav.lingjiao.base.tools;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.zjeav.lingjiao.base.config.BaseConfig;
import com.zjeav.lingjiao.base.utils.MediaFileUtil;
import okhttp3.ResponseBody;

public class FileUtils {

    public static boolean writeResponseBodyToDisk(Context context, ResponseBody body, String no, Handler handler) {
        try {
            //创建文件夹 MyDownLoad，在存储卡下
            String dirName = BaseConfig.FILE_DOWNLOAD;
            File file = new File(dirName);
            //不存在创建
            if (!file.exists()) {
                file.mkdir();
            }
            //下载后的文件名
            String downloadFilepath = dirName + no;
            File newFile = new File(downloadFilepath);
            if (!newFile.exists()) {
                newFile.createNewFile();
            }

            InputStream inputStream = null;
            OutputStream outputStream = null;

            try {
                byte[] fileReader = new byte[1024];

                inputStream = body.byteStream();
                outputStream = new FileOutputStream(newFile);

                int size = inputStream.read(fileReader);
                //写数据
                while (size != -1) {
                    if (size != 1024) {
                        outputStream.write(fileReader, 0, size);
                        outputStream.flush();
                    } else {
                        outputStream.write(fileReader, 0, size);
                        outputStream.flush();
                    }
                    size = inputStream.read(fileReader);
                }
                return true;
            } catch (IOException e) {
                return false;
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }

                if (outputStream != null) {
                    outputStream.close();
                }
                if (body != null) {
                    body.close();
                }
                Message message = new Message();
                message.what = 1;
                message.arg1 = 1;
                handler.sendMessage(message);

            }
        } catch (IOException e) {
            return false;
        }
    }

    //判断文件是否已下载
    public static boolean fileisDownload(String path) {
        boolean isdownload = false;
        File file = new File(path);
        if (!file.exists()) {
            isdownload = false;
        } else {
            isdownload = true;
        }
        return isdownload;
    }

    public static List<String> getFilesAllName(String path) {
        File file = new File(path);
        File[] files = file.listFiles();
        if (files == null) {
            Log.e("error", "空目录");
            return null;
        }
        List<String> s = new ArrayList<>();
        for (int i = 0; i < files.length; i++) {
            if (MediaFileUtil.isAudioFileType(files[i].getAbsolutePath())){
                s.add(files[i].getAbsolutePath());
            }
        }
        return s;
    }
    public static Long getRingDuring(String mUri){
        long duration = 0;
        android.media.MediaMetadataRetriever mmr = new android.media.MediaMetadataRetriever();

        try {
            if (mUri != null) {
                HashMap<String, String> headers=null;
                if (headers == null) {
                    headers = new HashMap<String, String>();
                    headers.put("User-Agent", "Mozilla/5.0 (Linux; U; Android 4.4.2; zh-CN; MW-KW-001 Build/JRO03C) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 UCBrowser/1.0.0.001 U4/0.8.0 Mobile Safari/533.1");
                }
                mmr.setDataSource(mUri, headers);
            }

             String    duration_str = mmr.extractMetadata(android.media.MediaMetadataRetriever.METADATA_KEY_DURATION);
             duration = Long.parseLong(duration_str);//毫秒数
        } catch (Exception ex) {
        } finally {
            mmr.release();
        }
        Log.e("ryan","duration "+duration);
        return duration;
    }


}
